View Javadoc
1   package org.apache.maven.surefire.junitcore.pc;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.surefire.report.ConsoleLogger;
23  import org.apache.maven.surefire.util.internal.DaemonThreadFactory;
24  import org.junit.runner.Description;
25  import org.junit.runners.model.RunnerScheduler;
26  
27  import java.util.Collection;
28  import java.util.concurrent.ConcurrentLinkedQueue;
29  import java.util.concurrent.ExecutorService;
30  import java.util.concurrent.LinkedBlockingQueue;
31  import java.util.concurrent.ThreadFactory;
32  import java.util.concurrent.ThreadPoolExecutor;
33  import java.util.concurrent.TimeUnit;
34  
35  /**
36   * Used to execute tests annotated with {@link net.jcip.annotations.NotThreadSafe}.
37   * <p/>
38   *
39   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
40   * @see ParallelComputerBuilder
41   * @since 2.18
42   */
43  final class SingleThreadScheduler
44  {
45      private final ConsoleLogger logger;
46  
47      private final ExecutorService pool = newPool();
48  
49      private final Scheduler master;
50  
51      private static ExecutorService newPool()
52      {
53          ThreadFactory tf = DaemonThreadFactory.newDaemonThreadFactory( "maven-surefire-plugin@NotThreadSafe" );
54          return new ThreadPoolExecutor( 1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), tf );
55      }
56  
57      SingleThreadScheduler( ConsoleLogger logger )
58      {
59          this.logger = logger;
60          SchedulingStrategy strategy = SchedulingStrategies.createParallelSharedStrategy( logger, pool );
61          master = new Scheduler( logger, null, strategy );
62      }
63  
64      RunnerScheduler newRunnerScheduler()
65      {
66          SchedulingStrategy strategy = SchedulingStrategies.createParallelSharedStrategy( logger, pool );
67          return new Scheduler( logger, null, master, strategy );
68      }
69  
70      /**
71       * @see Scheduler#describeStopped(boolean)
72       */
73      ShutdownResult describeStopped( boolean shutdownNow )
74      {
75          ShutdownResult shutdownResult = master.describeStopped( shutdownNow );
76          return new ShutdownResult( copyExisting( shutdownResult.getTriggeredTests() ),
77                                     copyExisting( shutdownResult.getIncompleteTests() ) );
78      }
79  
80      /**
81       * @see Scheduler#shutdownThreadPoolsAwaitingKilled()
82       */
83      boolean shutdownThreadPoolsAwaitingKilled()
84      {
85          return master.shutdownThreadPoolsAwaitingKilled();
86      }
87  
88      private Collection<Description> copyExisting( Collection<Description> descriptions )
89      {
90          Collection<Description> activeChildren = new ConcurrentLinkedQueue<Description>( descriptions );
91          ParallelComputerUtil.removeUnusedDescriptions( activeChildren );
92          return activeChildren;
93      }
94  }